package com.csy.mysql_util;

import java.sql.*;

public class MysqlUtil {
    private static String userName;
    private static String userPassword;
    private static String Name;
    private static Connection connection;
    private static Statement statement;
    private static ResultSet rs;
    private static String IP="192.168.43.17";
    public MysqlUtil()
    {

    }
    public MysqlUtil(String name,String username, String userpassword)
    {
        this.Name=name;
        this.userName=username;
        this.userPassword=userpassword;
    }

    public String getIp()
    {
        return this.IP;
    }

    public static Connection getConnection() throws Exception
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            String url = "jdbc:mysql://"+IP+":3306/test?characterEncoding=utf8";
            String user = "root";
            String password = "root";
            connection = DriverManager.getConnection(url, user, password);
            statement = connection.createStatement();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
        finally
        {
            return connection;
        }
    }
    public int checkAccout(StringBuffer name) throws ClassNotFoundException, SQLException
    {
        Connection connection = null;
        int flag=-1;
        try
        {
            connection = MysqlUtil.getConnection();
            ResultSet rs = statement.executeQuery("select * from user ");

            while (rs.next())
            {
                String username=rs.getString("userName");
                String userpasswod=rs.getString("userPassword");
//              System.out.println(username);
//              System.out.println(userpasswod);
                if(username.equals(this.userName))
                {
                    flag=0;
                    if(username.equals(this.userName)&&userpasswod.equals(this.userPassword))
                    {
                        flag=1;
                        name.append(rs.getString("Name"));
                    }
                }
            }
            try
            {
                MysqlUtil.close();

            }
            catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            return flag;
        }
    }

    public int CreatAccount() throws ClassNotFoundException, SQLException
    {
        int n=-1;
        Connection connection = null;
        try
        {
            connection = MysqlUtil.getConnection();
            int temp=new MysqlUtil(this.Name,this.userName,this.userPassword).checkAccout(new StringBuffer(this.Name));
            if(temp==0)
            {
                n=1;
                System.out.println("注册失败");
            }
            else
            {
                String username = this.userName;
                String userpassword = this.userPassword;
                String name = this.Name;
                if (!username.equals("") && !userpassword.equals("") && !name.equals("")) {
                    String sql = "INSERT INTO user (userName,userPassword,Name) values (?,?,?)";
                    PreparedStatement ps = connection.prepareStatement(sql);
                    ps.setString(1, this.userName);
                    ps.setString(2, this.userPassword);
                    ps.setString(3, this.Name);
                    ps.executeUpdate();
                    n=0;
                    System.out.println("注册成功");
                    MysqlUtil.close();
                } else {
                    n = 2;
                }
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            return n;
        }
    }


    public int Get_chat_content(StringBuffer time[],StringBuffer name[],StringBuffer talk[]) throws ClassNotFoundException, SQLException
    {
        StringBuffer temp[][]= new StringBuffer[30][];
        Connection connection = null;
        int i=0;
        try
        {
            connection = MysqlUtil.getConnection();
            ResultSet rs = statement.executeQuery("select * from chatting_records ");
            while (rs.next()&&i<800)
            {
                time[i]=new StringBuffer(rs.getString("time"));
                name[i]=new StringBuffer(rs.getString("name"));
                talk[i]=new StringBuffer(rs.getString("chat_content"));
                i++;
            }
            try
            {
                MysqlUtil.close();

            }
            catch (SQLException throwables)
            {
                throwables.printStackTrace();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            return i;
        }
    }


    public void set_chat_content(String time,StringBuffer name ,String chat_content) throws ClassNotFoundException, SQLException
    {
        Connection connection = null;
        try
        {
            String name1=new String(name);
            connection = MysqlUtil.getConnection();
            String sql="INSERT INTO chatting_records (time,name,chat_content) values (?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, time);
            ps.setString(2, name1);
            ps.setString(3, chat_content);
            ps.executeUpdate();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void  close()throws SQLException{
        if (rs != null) {
            rs.close();
        }
        if (statement != null) {
            statement.close();

        }
        if (connection != null) {
            connection.close();
        }
    }

}
